package com.office.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.office.base.BaseController;
import com.office.dao.model.Admin;
import com.office.service.AdminService;

@Controller
@RequestMapping("/admin")
public class Admincontroller extends BaseController {

	@Autowired
	AdminService adminService;

	// ---------------常用页面------
	// 登录
	@RequestMapping("/toLogin")
	public String toLogin(HttpServletRequest request, Map<String, Object> map) {
		Admin admin = (Admin) request.getSession().getAttribute("admin");
		if (admin != null) {
			return "redirect:/admin/index.html";
		}
		return jsp("public/login");
	}

	// 提交登录
	@RequestMapping("/postLogin")
	public String postLogin(Map<String, Object> map,
			HttpServletRequest request, String userName, String password,
			String scode) {
		Admin admin = adminService.login(userName, password);
		String seScode = (String) request.getSession().getAttribute(
				"admin-login-scode");
		request.getSession().removeAttribute("admin-login-scode");

		if (seScode == null) {
			map.put("error", "验证码已经失效!");
			return jsp("public/login");
		}
		if (!seScode.equals(scode)) {
			map.put("error", "验证码有误!");
			return jsp("public/login");
		}
		if (admin == null) {
			map.put("error", "用户名或密码错误!");
			return jsp("public/login");
		}

		request.getSession().setAttribute("admin", admin);
		return "redirect:/admin/index.html";
	}

	@RequestMapping("/index")
	public String index(HttpServletRequest request, Map<String, Object> map) {
		
		Admin admin = (Admin) request.getSession().getAttribute("admin");
		admin = adminService.selectByPrimaryKey(admin.getId().intValue());
		map.put("admin", admin);
		System.out.println(admin);
		return jsp("public/index");
	}

	/*
	 * 
	 * 
	 * 
	 * @RequestMapping("/loginOut") public String loginOut(HttpServletRequest
	 * request) { request.getSession().invalidate(); return
	 * "redirect:/admin/toLogin.html"; }
	 */

}
